package org.example.scoresystem_backen.util;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import jakarta.servlet.http.HttpServletRequest;

/**
 * 简化的认证工具类
 */
public class AuthUtil {

    /**
     * 获取当前用户ID
     */
    public static Integer getCurrentUserId() {
        HttpServletRequest request = getCurrentRequest();
        return request != null ? (Integer) request.getAttribute("userId") : null;
    }

    /**
     * 获取当前用户名
     */
    public static String getCurrentUsername() {
        HttpServletRequest request = getCurrentRequest();
        return request != null ? (String) request.getAttribute("username") : null;
    }

    /**
     * 获取当前用户类型
     */
    public static String getCurrentUserType() {
        HttpServletRequest request = getCurrentRequest();
        return request != null ? (String) request.getAttribute("userType") : null;
    }

    /**
     * 检查当前用户是否为管理员（包括普通管理员和超级管理员）
     */
    public static boolean isAdmin() {
        String userType = getCurrentUserType();
        return "admin".equals(userType) || "super_admin".equals(userType);
    }

    /**
     * 检查当前用户是否为超级管理员
     */
    public static boolean isSuperAdmin() {
        return "super_admin".equals(getCurrentUserType());
    }

    /**
     * 检查当前用户是否为学生
     */
    public static boolean isStudent() {
        return "student".equals(getCurrentUserType());
    }

    private static HttpServletRequest getCurrentRequest() {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        return attributes != null ? attributes.getRequest() : null;
    }
}
